/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaclientcp3110.UCM;
import common.UCM.UCController;
import common.Objects.Component;
import javaclientcp3110.Model.Component.CompEditModel;
import javaclientcp3110.Model.MainModel;
import javaclientcp3110.View.Component.CompEditView;

/**
 * UC Edit Component
 *
 * Precondition: A valid user is logged into the system.
 *
 *  1. User selects to edit a component.
 *  2. System displays the edit component view.
 *  3. User selects a type of component.
 *  4. System loads a list of components of that type.
 *  5. User selects component from the list.
 *  6. System displays the details of that component.
 *  7. User edits the details and clicks save.
 *  8. System updates the component details in the storage medium.
 *  9. System displays a success message.
 *
 * Postcondition: The component details are updated in the system.
 *
 * Unsuccessful Outcomes:
 *
 *   * 3a) User enters invalid data.
 *        1. System displays an error message
 *        2. System prompts user to try again
 *   * 8a) System is unable to update the storage medium
 *        1. System displays an error message
 * @author Richard Irons
 */
public class UCEditComponent implements UCController{
    private CompEditView view;
    private CompEditModel model;
    /**
     * Defualt constructor, creates new model and view
     */
    public UCEditComponent() {
        this (new CompEditView(), new CompEditModel());
    }
    /**
     * Constructor that sets the model and view to the inputs
     * @param view - view to set to
     * @param model - model to set to
     */
    public UCEditComponent(CompEditView view, CompEditModel model) {
        this.view = view;
        this.model = model;
    }
    /**
     * Implement uccontrollers run function. This is the body of the use case, it
     * is where the use case is run.
     */
    public void run() {
    //*  1. User selects to edit a component.
    //*  2. System displays the edit component view.
    //*  3. User selects a type of component.
    //*  4. System loads a list of components of that type.
    //*  5. User selects component from the list.        
    //*  6. System displays the details of that component.
    //*  7. User edits the details and clicks edit.
    //*  8. System updates the component details in the storage medium.
    
    //*   * 3a) User enters invalid data.
        if (validateComponent() == false) {
    //*        1. System displays an error message
    //*        2. System prompts user to try again
            view.errorMessage("The entered component details are not valid " +
                    "\n Please try again.");
        }
        else {
            Component tempComponent = new Component();
            if (view.getCompId()< 0){
                view.errorMessage("Nothing to edit");
            }
            else {
                tempComponent.setId(view.getCompId());
                tempComponent.setName(view.getCompName());
                tempComponent.setDescription(view.getCompDescription());
                tempComponent.setCurrentStock(view.getCompCurrentStock());
                tempComponent.setPrice(view.getCompPrice());
                tempComponent.setCategoryId(view.getCompCategory().getId());

                model.setEditComponent(tempComponent);
                boolean itemEdited = model.editComponent();

                if (itemEdited) {
    //*  9. System displays a success message.
                    view.successMessage("Component successfully edited!");
                    MainModel.getInstance().updateData();
                }
                else {
    //*   * 8a) System is unable to update the storage medium
    //*        1. System displays an error message
                    view.errorMessage("Component could not be edited");
                }
            }
        }
    }
    private boolean validateComponent() {
        if (view.getCompName().equals(null)) {
            return false;
        }
        if (view.getCompDescription().equals(null)) {
            return false;
        }
        try {
            view.getCompCurrentStock();
        }
        catch (NumberFormatException e) {
            return false;
        }
        try {
            view.getCompPrice();
        }
        catch (NumberFormatException e) {
            return false;
        }
        return true;
    }

}
